package com.chuzhiyun.chengguoxy.feature.internship.leave;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.FragmentLeaveListBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseFragment;
import com.chuzhiyun.chengguoxy.feature.bean.LeavelistBean;
import com.chuzhiyun.chengguoxy.feature.login.SmsCodeActivity;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.NewItemClickListener;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

public class LeaveListFragment extends BaseFragment<FragmentLeaveListBinding> {

    private LeaveListFragment.MyAdapter myAdapter;
    private final List<LeavelistBean.ResultsBean> dataSource = new ArrayList<LeavelistBean.ResultsBean>();

    @Override
    public void initView(View view) {
        binding.navView.ivBack.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                requireActivity().finish();
            }
        });
        binding.navView.tvTitle.setText("请假记录");
    }

    @Override
    public void initData() {
        getLeaveList();

        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        myAdapter = new LeaveListFragment.MyAdapter(R.layout.item_leavelist, dataSource);
        binding.recyclerView.setAdapter(myAdapter);
        @SuppressLint("InflateParams") View empty = LayoutInflater.from(getActivity()).inflate(R.layout.empty_view,null,false);
        myAdapter.setEmptyView(empty);
        myAdapter.notifyDataSetChanged();

        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
               getLeaveList();
            }
        });

        binding.refreshLayout.setEnableLoadMore(false);

        myAdapter.setOnItemClickListener(new NewItemClickListener() {
            @Override
            protected void onSingleClick(BaseQuickAdapter adapter, View view, int position) {
                LeavelistBean.ResultsBean resultsBean = dataSource.get(position);
                Intent intent = new Intent();
                intent.setClass(getContext(), LeaveInfoActivity.class);
                intent.putExtra("leaveId",resultsBean.getId());
                intent.putExtra("approval",resultsBean.getApproval());
                startActivity(intent);
            }
        });
    }

    @Override
    public void setListener() {

    }

    private void getLeaveList(){
        String url = NetApi.getLeavedaysOffList();
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(statusCode);
                dataSource.clear();
                binding.refreshLayout.finishRefresh();
                if (statusCode==200) {
                    Gson gson = new Gson();
                    LeavelistBean bean = gson.fromJson(response,LeavelistBean.class);
                    dataSource.addAll(bean.getResults());
                    myAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
                binding.refreshLayout.finishRefresh();
            }
        });
    }

    public static class MyAdapter extends BaseQuickAdapter<LeavelistBean.ResultsBean, BaseViewHolder> {

        public MyAdapter(int layoutResId, List<LeavelistBean.ResultsBean> data) {
            super(layoutResId, data);
        }

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        protected void convert(BaseViewHolder helper, LeavelistBean.ResultsBean item) {
            helper.setText(R.id.leaveIntervalLabel, "请假日期:"+item.getStart()+" 至 "+item.getEnd());
            String oldStr = item.getLastModified().substring(0,19);
            helper.setText(R.id.leave_updateTime,"提交日期:"+convertDate(oldStr));

            if (item.getApproval().equals("pending")){
                helper.setText(R.id.status_Text,"待审批");
                helper.setBackgroundResource(R.id.status_Text,R.drawable.leave_state_yello_color);
            }
            if (item.getApproval().equals("approved")){
                helper.setText(R.id.status_Text,"通过");
                helper.setBackgroundResource(R.id.status_Text,R.drawable.leave_state_green_color);
            }
            if (item.getApproval().equals("rejected")){
                helper.setText(R.id.status_Text,"不通过");
                helper.setBackgroundResource(R.id.status_Text,R.drawable.leave_state_red_color);
            }
        }
    }

    //将带有T的时间字符串转换成yyyy-MM-dd HH:mm:ss
    @RequiresApi(api = Build.VERSION_CODES.O)
    private static String convertDate(String strDate) {
        String str = strDate;
        str = str.replace("T"," ");
        return str;
    }

}